package cgs_map

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/base"
	"cgs-server/server/model"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"net/http"
	"strconv"
)

func init() {
	server.Get("/api/Cgs/Portal/CgsMap/list", listMap, "地图场景列表", server.Login)
	server.Get("/api/Cgs/Portal/CgsMap/detail", detailMap, "地图场景详情", server.None)
	server.Post("/api/Cgs/Portal/CgsMap/save", saveMap, "添加地图场景", server.Login)
	server.Post("/api/Cgs/Portal/CgsMap/update", updateMap, "更新地图场景", server.Login)
	server.Post("/api/Cgs/Portal/CgsMap/remove", removeMap, "删除地图场景", server.Login)
}

func listMap(w http.ResponseWriter, r *http.Request) {
	// 分页
	current, err:= strconv.ParseInt(r.FormValue("current"), 10, 64)
	if err != nil {
		current = 0
	}
	size, err	:= strconv.ParseInt(r.FormValue("size"), 10, 64)
	if err != nil {
		size = 50
	}

	mdl := &CgsMap{}
	duModel := base.GenerateDataUserModel(mdl, r)
	duModel.Page = &model.Page{
		Current: current,
		Size:    size,
	}
	visualService := NewMapService()
	res, err := visualService.List(duModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Get Successfully!").WithData(res))
}

func detailMap(w http.ResponseWriter, r *http.Request) {
	id, err := primitive.ObjectIDFromHex(r.FormValue("id"))
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	dataUserModel := base.GenerateDataUserModel(&CgsMap{Id: id}, r)
	mapService := NewMapService()
	res, err := mapService.GetOne(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Get successfully!").WithData(res))
}

func saveMap(w http.ResponseWriter, r *http.Request) {
	visualMap := &CgsMap{}
	err := helper.ReadRequestBody(visualMap, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	dataUserModel := base.GenerateDataUserModel(visualMap, r)
	mapService := NewMapService()
	err = mapService.Save(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Add successfully!").WithData(bson.M{"id": visualMap.Id}))
}

func updateMap(w http.ResponseWriter, r *http.Request) {
	visualMap := &CgsMap{}
	err := helper.ReadRequestBody(visualMap, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	dataUserModel := base.GenerateDataUserModel(visualMap, r)
	mapService := NewMapService()
	err = mapService.Update(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Add successfully!").WithData(bson.M{"id": visualMap.Id}))
}

func removeMap(w http.ResponseWriter, r *http.Request) {
	// 根据id删除
	id, err := primitive.ObjectIDFromHex(r.FormValue("id"))
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	dataUserModel := base.GenerateDataUserModel(&CgsMap{Id: id}, r)
	mapService := NewMapService()
	err = mapService.Remove(dataUserModel)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK("Delete successfully!"))
}
